﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity.Core;
using YFIN.Admin.Admin;
using YFIN.Admin.Helper;

namespace YFIN.Admin.ViewModels
{
    public class AdminLoginVM
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region 属性
        /// <summary>
        /// 用户名
        /// </summary>
        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    NotifyPropertyChanged("UserName");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    NotifyPropertyChanged("Password");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _oldPwd;
        public string OldPwd
        {
            get
            {
                return _oldPwd;
            }
            set
            {
                if (_oldPwd != value)
                {
                    _oldPwd = value;
                    NotifyPropertyChanged("OldPwd");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _newPwd;
        public string NewPwd
        {
            get
            {
                return _newPwd;
            }
            set
            {
                if (_newPwd != value)
                {
                    _newPwd = value;
                    NotifyPropertyChanged("NewPwd");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _newPwd2;
        public string NewPwd2
        {
            get
            {
                return _newPwd2;
            }
            set
            {
                if (_newPwd2 != value)
                {
                    _newPwd2 = value;
                    NotifyPropertyChanged("NewPwd2");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public AdminLogin login { get; set; }

        /// <summary>
        /// 绑定到的实现登录操作的命令
        /// </summary>
        public ICommand LoginCommand { get; private set; }
        #endregion

        public AdminLoginVM()
        {
            LoginCommand = new ActionCommand(Login);
        }

        public void Login()
        {
            try
            {
           login.UserBox.ClearValidationError();
           login.PasswordBox.ClearValidationError();
            bool isValid = true;
            if (login.UserBox.Text.Length == 0)
            {
                login.UserBox.ShowValidationError("用户名不能为空");
                isValid = false;
            }
            if (login.PasswordBox.Password.Length <= 5)
            {
                login.PasswordBox.ShowValidationError("密码必须大于等于6位");
                isValid = false;
            }
           
            if (isValid)
            {
                var client = new AdminServiceRef.AdminServiceClient();
                client.ExistCompleted += client_ExistCompleted;
                client.ExistAsync(UserName, login.PasswordBox.Password);
            }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void client_ExistCompleted(object sender, AdminServiceRef.ExistCompletedEventArgs e)
        {
            if(e.Result == null)
            {
                MessageBox.Show("用户名或密码错误！");
                //TODO
            }
            else
            {
                MainPage.Admin = e.Result;
                var adminView = new AdminView
                                    {
                                        tabItem1 =
                                            {Visibility = e.Result.Level ? Visibility.Collapsed : Visibility.Visible},
                                        tabControl1 = {SelectedIndex = e.Result.Level ? 1 : 0}
                                    };
                MainPage.TheMainPage.LayoutRoot.Children[0] = adminView;
            }
        }
    }
}
